package org.fly.controller.wap.customer;

import org.fly.bean.Customer;
import org.fly.bean.bo.CustomerBo;
import org.fly.bean.common.BusinessException;
import org.fly.contants.ErrorConstant;
import org.fly.controller.AbstractController;
import org.fly.enums.AuditStatus;
import org.fly.service.CustomerService;
import org.fly.utils.CookieUtil;
import org.fly.utils.JsonUtils;
import org.fly.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by rongzhao.zhu on 2018/4/8.
 */
@Controller
@RequestMapping("/wap/customer/")
public class WapCMainController extends AbstractController {
    @Autowired
    CustomerService customerService;
    @RequestMapping(value = "loginOn", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String login(String account,String password,
                        HttpServletRequest request,
                        HttpServletResponse response){
        Customer customer = null;
        try{
            customer =  customerService.login(account,password);
            CookieUtil.setLoginCookie(response,customer,"");
        }catch (BusinessException e){
            return toExceptionString(e);

        }
        return JsonUtils.encode(createDataTip(customer));
    }
    @RequestMapping(value = "registerOn", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String registerOn(CustomerBo data, HttpServletResponse response){
        Customer customer = null;
        try{
            customerService.register(data,data.getVerifyCode());
            customer =  customerService.login(data.getAccount(),data.getPassword());
            CookieUtil.setLoginCookie(response,customer,"");
        }catch (Exception e){
            return toExceptionString(e);
        }
        return toJSONString(createDataTip(customer));
    }

    @RequestMapping(value = "getUser", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getUser(HttpServletResponse response){
        Customer customer = (Customer) UserContext.getUser();
        return JsonUtils.encode(createDataTip(customer));
    }

    @RequestMapping(value = "update", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(Customer customer,HttpServletResponse response){
        Customer current = (Customer) UserContext.getUser();
        Customer db =  customerService.findById(current.getId());
        db.setHeadImage(customer.getHeadImage());
        db.setAddress(customer.getAddress());
        db.setName(customer.getName());
        current.setHeadImage(customer.getHeadImage());
        current.setAddress(customer.getAddress());
        current.setName(customer.getName());

        try {
            customerService.save(db);
        } catch (BusinessException e) {
            return toExceptionString(e);
        }
        return JsonUtils.encode(createDataTip(customer));
    }
    @RequestMapping(value = "certify", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String certify(Customer customer,HttpServletResponse response){
        Customer current = (Customer) UserContext.getUser();


        try {
            Customer db =  customerService.findById(current.getId());

            if(AuditStatus.pass.name().equals(db.getAuditStatus())){
                throw new BusinessException(ErrorConstant.ec_system_error,"已经审核通过!");
            }
            db.setAuditStatus(AuditStatus.init.name());
            db.setLicense(customer.getLicense());
            current.setAuditStatus(AuditStatus.init.name());
            current.setLicense(customer.getLicense());
            customerService.save(db);
        } catch (BusinessException e) {
            return toExceptionString(e);
        }
        return JsonUtils.encode(createDataTip(current));
    }

}
